package com.bdqn;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

/**
 * 使用DOM4J维护XML数据
 */
public class Demo04 {

    public static void main(String[] args) {
        Document doc = null;

        // 加载DOM树
        SAXReader saxReader = new SAXReader();
        try {
            doc = saxReader.read(new File("ch02/src/main/resources/维护收藏信息.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        // 获取XML的根节点
        Element root = doc.getRootElement();
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();) {
            Element brand = (Element) itBrand.next();
            // 输出标签的name属性
            System.out.println("品牌：" + brand.attributeValue("name"));
            // 遍历Type标签
            for (Iterator itType = brand.elementIterator(); itType.hasNext();) {
                Element type = (Element) itType.next();
                System.out.println("\t型号：" + type.attributeValue("name"));
            }
        }
    }
}
